<?php
/**
 * @title 5465. 子树中标签相同的节点数 - 递归
 * @author start2004
 */

class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $edges
     * @param String $labels
     * @return Integer[]
     */
    function countSubTrees($n, $edges, $labels) {
        /**
         * @since 2020-07-19 哈希节点和节点之间连线
         */
        $hashs = [];
        foreach ($edges as list($node1, $node2)){
            $hashs[$node1][] = $node2;
            $hashs[$node2][] = $node1;
        }

        /**
         * @since 2020-07-19 根->节点关系
         */
        $rootArray = [];
        $exists = [0];
        $currentNode = [0];
        while($currentNode){
            $nextNode = [];

            /**
             * @since 2020-07-19 遍历每层点
             */
            foreach ($currentNode as $root){
                /**
                 * @since 2020-07-19 遍历和root相连的边
                 */
                foreach ($hashs[$root] as $leaf){
                    if(!isset($exists[$leaf])){
                        $exists[$leaf] = 1;
                        $rootArray[$root][] = $leaf;
                        $nextNode[] = $leaf;
                    } else {}
                }
            }

            /**
             * @since 2020-07-19 下次循环
             */
            $currentNode = $nextNode;
        }
        // return $rootArray;
        
        /**
         * @since 2020-07-19 返回数据
         */
        $result = [];

        /**
         * @since 2020-07-19 每个节点统计情况
         */
        $nodeCountArray = [];

        /**
         * @since 2020-07-19 依次获取
         */
        for ($i=0; $i<$n; $i++){
            $result[] = self::find($i, $rootArray, $labels, $nodeCountArray);
        }

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 返回子树节点相同值数量
     * @author start2004
     * @since 2020-07-19 Created
     *
     * @param int $n
     * @param array $rootArray
     * @param string $labels
     * @param array $nodeCountArray
     */
    function find($n = 0, $rootArray = [], $labels = "", &$nodeCountArray = []){
        /**
         * @since 2020-07-19 节点值
         */
        $char = $labels[$n];

        /**
         * @since 2020-07-19 已统计
         */
        if(isset($nodeCountArray[$n])){
            return $nodeCountArray[$n][$char];
        } else {}

        /**
         * @since 2020-07-19 无子树
         */
        if(!isset($rootArray[$n])){
            $nodeCountArray[$n][$char] = 1;
            return 1;
        } else {}

        /**
         * @since 2020-07-19 遍历子节点
         */
        $countArray = [];
        foreach ($rootArray[$n] as $leaf){
            /**
             * @since 2020-07-19 已统计
             */
            if(isset($nodeCountArray[$leaf])){
            } else {
                /**
                 * @since 2020-07-19 未统计, 递归
                 */
                self::find($leaf, $rootArray, $labels, $nodeCountArray);
            }

            /**
             * @since 2020-07-19 合并统计数组
             */
            if(empty($countArray)){
                $countArray = $nodeCountArray[$leaf];
            } else {
                foreach ($nodeCountArray[$leaf] as $key=>$count){
                    if(isset($countArray[$key])){
                        $countArray[$key] += $count;
                    } else {
                        $countArray[$key] = $count;
                    }
                }
            }
        }

        /**
         * @since 2020-07-19 添加自身节点
         */
        if(isset($countArray[$char])){
            $countArray[$char] += 1;
        } else {
            $countArray[$char] = 1;
        }
        $nodeCountArray[$n] = $countArray;

        /**
         * @return
         */
        return $countArray[$char];
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.19_2.php
 */
$datas = [
    [4, [[0,2],[0,3],[1,2]], "aeed"],
    [7, [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], "abaedcd"],
    [4, [[0,1],[1,2],[0,3]], "bbbb"],
    [5, [[0,1],[0,2],[1,3],[0,4]], "aabab"],
    [6, [[0,1],[0,2],[1,3],[3,4],[4,5]], "cbabaa"],
    [7, [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6]], "aaabaaa"],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->countSubTrees($data[0], $data[1], $data[2]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
